// Package amap provides functions for interacting with the AMap API.
package go_amap

import (
	"github.com/duke-git/lancet/v2/condition"
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/duke-git/lancet/v2/strutil"
	"io"
	"net/url"
)

const (
	staticMapUrl = "https://restapi.amap.com/v3/staticmap"
)

type StaticMap struct {
	client *AmapClient
}

type StaticMapRequest struct {
	Location string
	Zoom     int
	Size     string
	Scale    int
	Markers  string
	Labels   string
	Paths    string
	Traffic  bool
}

type StaticMapResponse struct {
	Response
}

func (l *StaticMap) Do(request *StaticMapRequest) ([]byte, error) {
	val := url.Values{}

	if !strutil.IsBlank(request.Location) {
		val.Set("location", request.Location)
	}

	if request.Zoom > 0 {
		val.Set("zoom", convertor.ToString(request.Zoom))
	}

	if strutil.IsBlank(request.Size) {
		request.Size = "400*400"
	}
	if request.Scale > 0 {
		val.Set("scale", convertor.ToString(request.Scale))
	}

	if !strutil.IsBlank(request.Markers) {
		val.Set("markers", request.Markers)
	}
	if !strutil.IsBlank(request.Labels) {
		val.Set("labels", request.Labels)
	}
	if !strutil.IsBlank(request.Paths) {
		val.Set("paths", request.Paths)
	}
	val.Set("traffic", condition.TernaryOperator(request.Traffic, "1", "0"))
	val.Set("size", request.Size)

	resp, err := l.client.DoRequest(staticMapUrl, "GET", val)

	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	return io.ReadAll(resp.Body)
}
